package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserThreadLocal;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author Pan Rundong
 * @Date 2023-02-26 9:47
 * @Desc UserInfo控制器
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    /**
     * 根据id查询用户详细信息
     * @Author: Pan Rundong
     * @param id
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.UserInfoVo>
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> getUserInfo(Long id) {
        //如果没有传则调用自己
        if (id == null) {
            id = UserThreadLocal.getUserId();
        }

        //1. 调用service
        UserInfoVo userInfoVo = userInfoService.getById(id);
        //2. 返回值
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户详细信息
     * @Author: Pan Rundong
     * @param userInfo
     * @return org.springframework.http.ResponseEntity
     */
    @PutMapping
    public ResponseEntity update(UserInfo userInfo) {
        //1.从线程中取Id存入userInfo
        Long userId = UserThreadLocal.getUserId();
        userInfo.setId(userId);

        //2.调用service
        userInfoService.update(userInfo);

        //3.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号1-发送验证码
     *
     * @return org.springframework.http.ResponseEntity
     * @Author Pan Rundong
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //1.获得手机号
        String phone = UserThreadLocal.getPhone();
        //2.调用service方法
        userInfoService.sendCode(phone);
        //3.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号2-验证验证码
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     * @Author Pan Rundong
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> map) {
        //1.获取参数
        String verificationCode = map.get("verificationCode");
        String phone = UserThreadLocal.getPhone();
        //2.调用service方法
        userInfoService.verification(verificationCode, phone);
        //3.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号3-保存手机号
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     * @Author Pan Rundong
     */
    public ResponseEntity updatePhone(@RequestBody Map<String, String> map) {
        //1.获取参数
        String phone = map.get("phone");
        //2.调用service
        userInfoService.updatePhone(phone);
        //3.返回
        return ResponseEntity.ok(null);
    }
}
